/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package JPAControllers;

import JPAControllers.exceptions.NonexistentEntityException;
import Persistencia.Comentariovideo;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Persistencia.Sesion;
import Persistencia.Fisioterapeuta;

/**
 *
 * @author veinti
 */
public class ComentariovideoJpaController {

    public ComentariovideoJpaController() {
        emf = Persistence.createEntityManagerFactory("fisiosoftJDVPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Comentariovideo comentariovideo) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Sesion sesion = comentariovideo.getSesion();
            if (sesion != null) {
                sesion = em.getReference(sesion.getClass(), sesion.getIdSesion());
                comentariovideo.setSesion(sesion);
            }
            Fisioterapeuta fisioterapeuta = comentariovideo.getFisioterapeuta();
            if (fisioterapeuta != null) {
                fisioterapeuta = em.getReference(fisioterapeuta.getClass(), fisioterapeuta.getIdFisioterapeuta());
                comentariovideo.setFisioterapeuta(fisioterapeuta);
            }
            em.persist(comentariovideo);
            if (sesion != null) {
                sesion.getComentariovideoList().add(comentariovideo);
                sesion = em.merge(sesion);
            }
            if (fisioterapeuta != null) {
                fisioterapeuta.getComentariovideoList().add(comentariovideo);
                fisioterapeuta = em.merge(fisioterapeuta);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Comentariovideo comentariovideo) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Comentariovideo persistentComentariovideo = em.find(Comentariovideo.class, comentariovideo.getIdComentariovideo());
            Sesion sesionOld = persistentComentariovideo.getSesion();
            Sesion sesionNew = comentariovideo.getSesion();
            Fisioterapeuta fisioterapeutaOld = persistentComentariovideo.getFisioterapeuta();
            Fisioterapeuta fisioterapeutaNew = comentariovideo.getFisioterapeuta();
            if (sesionNew != null) {
                sesionNew = em.getReference(sesionNew.getClass(), sesionNew.getIdSesion());
                comentariovideo.setSesion(sesionNew);
            }
            if (fisioterapeutaNew != null) {
                fisioterapeutaNew = em.getReference(fisioterapeutaNew.getClass(), fisioterapeutaNew.getIdFisioterapeuta());
                comentariovideo.setFisioterapeuta(fisioterapeutaNew);
            }
            comentariovideo = em.merge(comentariovideo);
            if (sesionOld != null && !sesionOld.equals(sesionNew)) {
                sesionOld.getComentariovideoList().remove(comentariovideo);
                sesionOld = em.merge(sesionOld);
            }
            if (sesionNew != null && !sesionNew.equals(sesionOld)) {
                sesionNew.getComentariovideoList().add(comentariovideo);
                sesionNew = em.merge(sesionNew);
            }
            if (fisioterapeutaOld != null && !fisioterapeutaOld.equals(fisioterapeutaNew)) {
                fisioterapeutaOld.getComentariovideoList().remove(comentariovideo);
                fisioterapeutaOld = em.merge(fisioterapeutaOld);
            }
            if (fisioterapeutaNew != null && !fisioterapeutaNew.equals(fisioterapeutaOld)) {
                fisioterapeutaNew.getComentariovideoList().add(comentariovideo);
                fisioterapeutaNew = em.merge(fisioterapeutaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = comentariovideo.getIdComentariovideo();
                if (findComentariovideo(id) == null) {
                    throw new NonexistentEntityException("The comentariovideo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Comentariovideo comentariovideo;
            try {
                comentariovideo = em.getReference(Comentariovideo.class, id);
                comentariovideo.getIdComentariovideo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The comentariovideo with id " + id + " no longer exists.", enfe);
            }
            Sesion sesion = comentariovideo.getSesion();
            if (sesion != null) {
                sesion.getComentariovideoList().remove(comentariovideo);
                sesion = em.merge(sesion);
            }
            Fisioterapeuta fisioterapeuta = comentariovideo.getFisioterapeuta();
            if (fisioterapeuta != null) {
                fisioterapeuta.getComentariovideoList().remove(comentariovideo);
                fisioterapeuta = em.merge(fisioterapeuta);
            }
            em.remove(comentariovideo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Comentariovideo> findComentariovideoEntities() {
        return findComentariovideoEntities(true, -1, -1);
    }

    public List<Comentariovideo> findComentariovideoEntities(int maxResults, int firstResult) {
        return findComentariovideoEntities(false, maxResults, firstResult);
    }

    private List<Comentariovideo> findComentariovideoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Comentariovideo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Comentariovideo findComentariovideo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Comentariovideo.class, id);
        } finally {
            em.close();
        }
    }

    public int getComentariovideoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Comentariovideo> rt = cq.from(Comentariovideo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
